package boot

import (
	"context"
	"dc-go/global"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func Boot() {
	server := &http.Server{
		Addr:    fmt.Sprintf(":%d", global.DcConfig.System.Port),
		Handler: Router(),
	}
	go func() {
		if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			log.Fatalf("【dc-go】监听: %s\n", err)
		}
	}()
	DcStartup(fmt.Sprintf(":%d", global.DcConfig.System.Port))
	quit := make(chan os.Signal, 1)

	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit
	log.Println("【dc-go】开始退出...")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	if err := server.Shutdown(ctx); err != nil {
		log.Fatal("【dc-go】强制退出: ", err)
	}

	log.Println("【dc-go】退出完成!")
}

func DcStartup(port string) {
	fmt.Println("---------------------------------")
	fmt.Println("【dc-go】蚌埠电信数据门户2022版-后端系统")
	fmt.Println("智慧营销中心/IT支撑中心 王旭晨")
	fmt.Println("技术栈: Golang、Gin、Gorm")
	fmt.Println("系统已启动,开始监听 " + port + " 端口...")
	global.DcLogger.Info("蚌埠电信数据门户2022版-后端系统已启动,开始监听 " + port + " 端口...")
}
